﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Facebook;
using System.Windows.Threading;
using System.Collections.Generic;
namespace SVLvsXNA.Facebook
{
    public class FacebookServices
    {
        private static FacebookServices _instance = null;
        public const string ExtendedPermissions = "publish_stream, user_about_me,offline_access,read_stream,friends_about_me,friends_birthday,friends_education_history,friends_location,read_requests,user_photos,user_notes,user_work_history,user_religion_politics,user_relationships,user_religion_politics,user_education_history,user_birthday,user_likes,email";
        public const string AppID = "622269994463091";
        public const string AppSecrect = "f160143791b275cd7fe8dbef52eb4415";

        public JsonObject _personInfo { get; set; }

        private string _accessToken;
        public string AccessToken
        {
            get { return _accessToken; }
            set
            {
                _accessToken = value;
                getAppToken();
            }
        }

        public string AppToken { get; set; }

        public delegate void FacebookCallbackDelegate(object o, FacebookApiEventArgs e);


        private FacebookServices()
        {
            _accessToken = null;
            AppToken = null;
        }

        public static FacebookServices GetInstanse()
        {
            if (_instance == null)
                _instance = new FacebookServices();
            return _instance;
        }

        private void getAppToken()
        {
            FacebookClient fbClient = new FacebookClient(_accessToken);
            string url = "oauth/access_token?client_id="+AppID+"&client_secret="+AppSecrect+"&grant_type=client_credentials";
            fbClient.GetCompleted += (o, ex) =>
            {
                if (ex.Error != null)
                {
                    throw new ArgumentNullException();
                    this.AppToken = null;
                    return;
                }
                var result = (IDictionary<string, object>)ex.GetResultData();

                this.AppToken = (string)result["access_token"];
            };
            fbClient.GetAsync(url);
        }

        public void GetAsync(string url, object parameters, FacebookCallbackDelegate callBack)
        {
            FacebookClient fbClient = new FacebookClient(_accessToken);
            fbClient.GetCompleted += new EventHandler<FacebookApiEventArgs>(callBack);
            if (parameters == null)
                fbClient.GetAsync(url);
            else
                fbClient.GetAsync(url, parameters);
        }
        public void GetAsync(string url, FacebookCallbackDelegate callBack)
        {
            GetAsync(url, null, callBack);
        }

        public void PostAsync(string url, object parameters, FacebookCallbackDelegate callBack)
        {
            FacebookClient fbClient = new FacebookClient(_accessToken);
            fbClient.PostCompleted += new EventHandler<FacebookApiEventArgs>(callBack);
            fbClient.PostAsync(url, parameters);
        }

        public void DeleteAsync(string url, object parameters, FacebookCallbackDelegate callBack)
        {
            FacebookClient fbClient = new FacebookClient(_accessToken);
            fbClient.DeleteCompleted += new EventHandler<FacebookApiEventArgs>(callBack);
            if (parameters == null)
                fbClient.DeleteAsync(url);
        }

    }
}
